package com.dzt.androidframework.json.car;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.adapter.AbsListAdapter;
import com.dzt.androidframework.widgets.PinnedHeaderListView;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * Created by M02323 on 2017/9/26.
 */

public class CarAdapter extends AbsListAdapter<CarBrand>
		implements PinnedHeaderListView.PinnedHeaderAdapter,
		AbsListView.OnScrollListener,
		SectionIndexer {
	private int mLocationPosition = -1;

	public CarAdapter(Context context, List<CarBrand> datas) {
		super(context, datas);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = layoutInflater.inflate(R.layout.item_brand, parent, false);
			x.view().inject(holder, convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		CarBrand brand = datas.get(position);
		//holder.ivLogo.setImageBitmap(menu.getIcon());
		//x.image().bind(holder.ivIcon, "url", options); //加载url图片
		int section = getSectionForPosition(position);
		if (getPositionForSection(section) == position) {
			holder.llHeader.setVisibility(View.VISIBLE);
			char ch = brand.firstLetter.charAt(0);
			holder.tvHeader.setText(String.valueOf(ch));
		} else {
			holder.llHeader.setVisibility(View.GONE);
		}
		holder.tvContent.setText(brand.name);
		return convertView;
	}

	@Override
	public void onScrollStateChanged(AbsListView absListView, int i) {

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
						 int visibleItemCount, int totalItemCount) {
		if (view instanceof PinnedHeaderListView) {
			((PinnedHeaderListView) view).configureHeaderView(firstVisibleItem);
		}
	}

	@Override
	public Object[] getSections() {
		return new Object[0];
	}

	@Override
	public int getPositionForSection(int section) {
		for (int i = 0; i < getCount(); i++) {
			char firstChar = datas.get(i).firstLetter.charAt(0);
			if (firstChar == section) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public int getSectionForPosition(int position) {
		if (datas.size() <= position)
			return 0;
		return datas.get(position).firstLetter.charAt(0);
	}

	@Override
	public int getPinnedHeaderState(int position) {
		if (position < 0
				|| (mLocationPosition != -1 && mLocationPosition == position)
				|| datas.size() <= position) {
			return PINNED_HEADER_GONE;
		}
		mLocationPosition = -1;
		int nextSection = getSectionForPosition(position + 1);
		int nextSectionPosition = getPositionForSection(nextSection);
		if (nextSectionPosition != -1
				&& position == nextSectionPosition - 1) {
			return PINNED_HEADER_PUSHED_UP;
		}
		return PINNED_HEADER_VISIBLE;
	}

	@Override
	public void configurePinnedHeader(View header, int position, int alpha) {
		if (datas.size() <= position)
			return;
		char ch = datas.get(position).firstLetter.charAt(0);
		((TextView) header.findViewById(R.id.tv_list_header)).setText(String
				.valueOf(ch));
	}

	class ViewHolder {
		// 列表头布局
		@ViewInject(R.id.list_header)
		public LinearLayout llHeader;
		// 列表头文字
		@ViewInject(R.id.tv_list_header)
		public TextView tvHeader;
		// 列表内容文字
		@ViewInject(R.id.tv_name)
		public TextView tvContent;
		// 车标图片
		@ViewInject(R.id.iv_logo)
		public ImageView ivLogo;
	}
}
